<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <el-button type="primary" :icon="Plus"  @click="dialogFormVisible = true">添加</el-button>
      </div>
    </template>
    <!-- 表格 -->
    <el-table v-loading="loading" :data="trademarkList" border style="width: 100%">
      <el-table-column align="center" type="index" label="序号" width="80px" />
      <el-table-column prop="tmName" label="品牌名称" />
      <el-table-column label="品牌LOGO">
      <template v-slot="{row}">
        <img :src="row.logoUrl" style="width:100px;height:60px">
      </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template v-slot="{row,$index}">
          <el-button title="修改" type="warning" size="small" :icon="Edit" />
          <el-button title="删除" type="danger" size="small" :icon="Delete" @click="removeTrademark(row)" />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      style="margin-top:20px"
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 12]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="prev, pager, next, jumper,->,sizes,total " 
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 添加品牌时的对话框 -->
     <el-dialog v-model="dialogFormVisible" title="添加品牌">
    <el-form>
      <el-form-item label="品牌名称">
        <el-input v-model="trademark.tmName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="品牌LOGO">
         <el-upload
          class="avatar-uploader"
          :action="`${BASE_URL}/admin/product/upload`"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="trademark.logoUrl" :src="trademark.logoUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          <template #tip>
          <div class="el-upload__tip">
            只能上传jpg/jpeg/png文件,且不超过2MB
            </div>
        </template>
        </el-upload>
        
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="btnTrademark"
          >确定</el-button
        >
      </span>
    </template>
  </el-dialog>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "Trademark",
};
</script>
<script lang="ts" setup>
import { ref,reactive, onMounted } from "vue";
import { Plus,Edit, Delete } from "@element-plus/icons-vue";
import { reqGetTrademarkListData, reqSaveTrademarkInfo } from '@/api/product/trademark';
import type { TrademarkListModel, TrademarkMode,} from '@/api/product/model/trademarkModel'
import { ElMessage ,ElMessageBox} from 'element-plus'
import type { UploadProps } from 'element-plus'
const imageUrl = ref('')
// 定义数据
const currentPage = ref<number>(1)  // 当前页码 默认是1
const pageSize = ref<number>(3) // 每页显示多少条数据
const total = ref<number>(0) // 品牌总数量
const trademarkList = ref()
 // 上传图片地址，会从.env.development中加载
// console.log('ii',import.meta)
const BASE_URL = import.meta.env.VITE_API_URL
// 分页器相关的数据
const small = ref<boolean>(false)
const background = ref<boolean>(false)
const disabled = ref<boolean>(false)

// 弹出框相关的数据
const dialogFormVisible = ref<boolean>(false)

// 定义一个要发送给服务器的品牌数据
const initTrademark = ()=>({tmName:'',logoUrl:''})
const trademark = reactive<TrademarkMode>(initTrademark())


//  添加一个加载中loading
const loading = ref<boolean>(true)
 
const getTrademarkListData =async ()=>{
  const result = await reqGetTrademarkListData(currentPage.value,pageSize.value)
    trademarkList.value = result.records
    total.value = result.total
    loading.value = false

}
onMounted(()=>{
  getTrademarkListData()
})

// 3. 单击当前页码实现分页
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTrademarkListData()
}
// 4. 修改每页显示的条数
const handleSizeChange = (val: number) => {
  pageSize.value = val
  currentPage.value = 1
  getTrademarkListData()
}
// 添加数据的两个函数
// 5
const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
  uploadFile
) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!)
  trademark.logoUrl = response.data
}
// 6. 上传时的校验
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  const imgTypes = ['image/jpg','image/jpeg','image/png']
  if (!imgTypes.includes(rawFile.type)) {
    ElMessage.error('上传的图片只能是jpg/jpeg/png格式,请重新选择!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('图片的大小不能超过2MB!')
    return false
  }
  return true
}
// 7. 弹出框的确定按钮
const btnTrademark = async()=>{
  await reqSaveTrademarkInfo(trademark)
  // 提示添加成功
  ElMessage.success('添加数据成功')
  // 添加成功后关闭对话框
  dialogFormVisible.value = false
  // 跳转到第一页
  currentPage.value = 1
 
  // 重新发送请求
  getTrademarkListData()
}
// 8. 删除品牌u

const removeTrademark = (row: TrademarkMode) => {
  console.log('rr',row)
  // 删除品牌时的弹框
    ElMessageBox.confirm(`您真的要删除${row.tmName}吗?`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
      .then(() => {
        ElMessage({
          type: 'success',
          message: '删除成功'
        })
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消删除'
        })
      })
}
</script>


<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>